package com.lwh.jxl.service.impl;

import com.lwh.common.uniqueuekey.IdWorker;
import com.lwh.easypoi.util.EasyPoiUtil;
import com.lwh.jxl.dao.AdCheliangMapper;
import com.lwh.jxl.entity.AdCheliangEntity;
import com.lwh.jxl.service.AdCheliangService;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.*;


@Service
public class AdCheliangServiceImpl implements AdCheliangService {

    @Autowired
    private AdCheliangMapper adCheliangMapper;



    @Override
    public Map<String, Object> findPage(AdCheliangEntity query)  {
        int total = adCheliangMapper.findPageCount(query);
        List<AdCheliangEntity> list = adCheliangMapper.findPage(query);
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("total", total);
        map.put("rows", list);
        return map;

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int insertSelective(AdCheliangEntity mdCheliangEntity) {
        IdWorker idWorker=new IdWorker(0,0);
        String id = String.valueOf(idWorker.nextId());
        mdCheliangEntity.setId(id);
        mdCheliangEntity.setCreateDate(new Date());
        adCheliangMapper.insertSelective(mdCheliangEntity);

        return 1;
    }

    @Override
    public int updateByPrimaryKeySelective(AdCheliangEntity mdCheliangEntity) {
        return adCheliangMapper.updateByPrimaryKeySelective(mdCheliangEntity);
    }

    @Override
    public int deleteByIds(String[] ids) {
        return adCheliangMapper.deleteMore(ids);
    }

    @Override
    public int deleteOneById(String id) {
        return adCheliangMapper.deleteOne(id);
    }

    // 原生jxl导入
    @Override
    public String importExcel(MultipartFile uploadFile) {
        List<AdCheliangEntity> list = new ArrayList<AdCheliangEntity>();
        IdWorker idWorker=new IdWorker(0,0);
        try {
            // 1.获取用户上传的文件
            Workbook workbook = Workbook.getWorkbook(uploadFile.getInputStream());
            // 2.获取工作簿sheet
            Sheet sheet = workbook.getSheet(0);
            // 3.获取总行数
            int rows = sheet.getRows();
            // 4.循环获取每一行数据
            for (int i = 1; i < rows; i++) {
                AdCheliangEntity adCheliangEntity = new AdCheliangEntity();
                String chepaihao = sheet.getCell(1, i).getContents();
                String chexing = sheet.getCell(2, i).getContents();
                String zuidatiji = sheet.getCell(3, i).getContents();
                String zaizhong = sheet.getCell(4, i).getContents();
                String zairen = sheet.getCell(5, i).getContents();
                String jiazhao = sheet.getCell(6, i).getContents();
                String zhuangtai = sheet.getCell(7, i).getContents();
                String beizhu = sheet.getCell(8, i).getContents();
                String username = sheet.getCell(9, i).getContents();
                String gpsid = sheet.getCell(10, i).getContents();
                String id = String.valueOf(idWorker.nextId());

                // 赋值
                adCheliangEntity.setId(id);
                adCheliangEntity.setChepaihao(chepaihao);
                adCheliangEntity.setChexing(chexing);
                adCheliangEntity.setZuidatiji(zuidatiji);
                adCheliangEntity.setZaizhong(zaizhong);
                adCheliangEntity.setZairen(zairen);
                adCheliangEntity.setJiazhao(jiazhao);
                adCheliangEntity.setZhuangtai(zhuangtai);
                adCheliangEntity.setBeizhu(beizhu);
                adCheliangEntity.setUsername(username);
                adCheliangEntity.setGpsid(gpsid);

                list.add(adCheliangEntity);
            }
            // 4.添加到数据库中
            adCheliangMapper.importExcel(list);
            // 5.关闭资源
            workbook.close();
            return  "导入成功";
        }catch(Exception e){
            return  "导入失败";
        }

    }

    @Override
    public List<AdCheliangEntity> outExcel(AdCheliangEntity mdCheliangEntity) {

        List<AdCheliangEntity> list = adCheliangMapper.outExcel(mdCheliangEntity);
        return list;


    }

    @Override
    public List<AdCheliangEntity> findAll() throws Exception {
        return null;
    }

}
